package com.young.common.expression.dns;

import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 12:41 PM
 */
public class ThreadLocalResolverSupporter {
    private static final ThreadLocal<Map<String, String>> DOMAIN_RESOLVER_MAPPER = new ThreadLocal();
    private static final ThreadLocal<Map<String, String>> SERVICE_RESOLVER_MAPPER = new ThreadLocal();
    private static final ThreadLocal<Map<String, String>> SERVICE_GROUP_RESOLVER_MAPPER = new ThreadLocal();
    private static final ThreadLocal<DnsResolverTransmittableEnum> TRANSMITTABLE = new ThreadLocal();

    public ThreadLocalResolverSupporter() {
    }

    public static String findDomain(String target) {
        return CachedMatcher.find((Map)DOMAIN_RESOLVER_MAPPER.get(), target);
    }

    public static String findService(String target) {
        return CachedMatcher.find((Map)SERVICE_RESOLVER_MAPPER.get(), target.toUpperCase());
    }

    public static String findServiceGroup(String target) {
        return CachedMatcher.find((Map)SERVICE_GROUP_RESOLVER_MAPPER.get(), target.toUpperCase());
    }

    public static void setDomainResolver(String domain, String target) {
        if (!StringUtils.isBlank(domain) && !StringUtils.isBlank(target)) {
            Map<String, String> context = (Map)DOMAIN_RESOLVER_MAPPER.get();
            if (context == null) {
                context = new LinkedHashMap();
                DOMAIN_RESOLVER_MAPPER.set(context);
            }

            ((Map)context).put(domain, target);
        }
    }

    public static void setServiceResolver(String service, String target) {
        if (!StringUtils.isBlank(service) && !StringUtils.isBlank(target)) {
            Map<String, String> context = (Map)SERVICE_RESOLVER_MAPPER.get();
            if (context == null) {
                context = new LinkedHashMap();
                SERVICE_RESOLVER_MAPPER.set(context);
            }

            ((Map)context).put(service, target);
        }
    }

    public static void setServiceGroupResolver(String service, String target) {
        if (!StringUtils.isBlank(service) && !StringUtils.isBlank(target)) {
            Map<String, String> context = (Map)SERVICE_GROUP_RESOLVER_MAPPER.get();
            if (context == null) {
                context = new LinkedHashMap();
                SERVICE_GROUP_RESOLVER_MAPPER.set(context);
            }

            ((Map)context).put(service, target);
        }
    }

    public static void setDomainResolver(String source) {
        String[] segments = StringUtils.split(source, ",");
        if (segments != null && segments.length != 0) {
            String[] var2 = segments;
            int var3 = segments.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String segment = var2[var4];
                String[] kv = StringUtils.split(segment, "=");
                if (kv != null && kv.length == 2) {
                    setDomainResolver(kv[0].trim(), kv[1].trim());
                }
            }

        }
    }

    public static void setServiceResolver(String source) {
        String[] segments = StringUtils.split(source, ",");
        if (segments != null && segments.length != 0) {
            String[] var2 = segments;
            int var3 = segments.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String segment = var2[var4];
                String[] kv = StringUtils.split(segment, "=");
                if (kv != null && kv.length == 2) {
                    setServiceResolver(kv[0].trim(), kv[1].trim());
                }
            }

        }
    }

    public static void setServiceGroupResolver(String source) {
        String[] segments = StringUtils.split(source, ",");
        if (segments != null && segments.length != 0) {
            String[] var2 = segments;
            int var3 = segments.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String segment = var2[var4];
                String[] kv = StringUtils.split(segment, "=");
                if (kv != null && kv.length == 2) {
                    setServiceGroupResolver(kv[0].trim(), kv[1].trim());
                }
            }

        }
    }

    public static Map<String, String> getDomainResolvers() {
        Map<String, String> result = new LinkedHashMap();
        if (DOMAIN_RESOLVER_MAPPER.get() != null) {
            result.putAll((Map)DOMAIN_RESOLVER_MAPPER.get());
        }

        return result;
    }

    public static String getDomainResolver(String domain) {
        return DOMAIN_RESOLVER_MAPPER.get() != null ? (String)((Map)DOMAIN_RESOLVER_MAPPER.get()).get(domain) : null;
    }

    public static String getServiceResolver(String service) {
        return SERVICE_RESOLVER_MAPPER.get() != null ? (String)((Map)SERVICE_RESOLVER_MAPPER.get()).get(service) : null;
    }

    public static Map<String, String> getServiceResolvers() {
        Map<String, String> result = new LinkedHashMap();
        if (SERVICE_RESOLVER_MAPPER.get() != null) {
            result.putAll((Map)SERVICE_RESOLVER_MAPPER.get());
        }

        return result;
    }

    public static String getServiceGroupResolver(String service) {
        return SERVICE_GROUP_RESOLVER_MAPPER.get() != null ? (String)((Map)SERVICE_GROUP_RESOLVER_MAPPER.get()).get(service) : null;
    }

    public static Map<String, String> getServiceGroupResolvers() {
        Map<String, String> result = new LinkedHashMap();
        if (SERVICE_GROUP_RESOLVER_MAPPER.get() != null) {
            result.putAll((Map)SERVICE_GROUP_RESOLVER_MAPPER.get());
        }

        return result;
    }

    public static void setTransmittable(DnsResolverTransmittableEnum transmittable) {
        TRANSMITTABLE.set(transmittable);
    }

    public static DnsResolverTransmittableEnum getTransmittable() {
        DnsResolverTransmittableEnum transmittable = (DnsResolverTransmittableEnum)TRANSMITTABLE.get();
        return transmittable == null ? DnsResolverTransmittableEnum.DEFAULT : transmittable;
    }

    public static void clear() {
        DOMAIN_RESOLVER_MAPPER.remove();
        SERVICE_RESOLVER_MAPPER.remove();
        SERVICE_GROUP_RESOLVER_MAPPER.remove();
        TRANSMITTABLE.remove();
    }
}
